package com.fau.cm.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCall;
import com.fau.cm.entity.MedicalCallBlacklist;
import com.fau.cm.entity.MedicalCallCustomer;
import com.fau.cm.entity.MedicalCallEnd;
import com.fau.cm.entity.MedicalCallExtension;
import com.fau.cm.entity.MedicalCallPending;
import com.fau.cm.entity.MedicalCallStandardArchives;
import com.fau.cm.entity.MedicalCallTelInfo;
import com.fau.cm.entity.MedicalCallTerminate;
import com.fau.cm.entity.MedicalCallUntreated;
import com.fau.cm.entity.MedicalEnterpriseOutside;
import com.fau.cm.entity.MedicalKeyValue;
import com.fau.cm.entity.MedicalOrdersStorage;
import com.fau.cm.service.ICallMyManagerService;
import com.fau.cm.util.Page;
import com.fau.cm.util.ParamUtil;
import com.fau.cm.util.SerialNumUtil;
import com.fau.cm.util.SystemConfig;

@Service
@Transactional(readOnly = true)
public class CallMyManagerServiceImpl extends BaseService implements
		ICallMyManagerService {

	@SuppressWarnings("unchecked")
	@Override
	public Page findCallUntreatedByPage(MedicalCallUntreated call,
			MedicalAdmin admin, Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallUntreated a where 1=1 ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallUntreated a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? and a.grade=? ");
			countSql.append("and a.hospitalId=? and a.grade=? ");
			objectList.add(admin.getEnterpriseId());
			objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		} else {
			statusSql.append("and a.handleUserId=? ");
			countSql.append("and a.handleUserId=? ");
			objectList.add(admin.getId());
		}
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}	
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallUntreated> callUntreatedList = new ArrayList<MedicalCallUntreated>();
		for(Object obj : page.getResult()) {
			MedicalCallUntreated bean = (MedicalCallUntreated) obj;
			if(StringUtils.isNotBlank(bean.getCreateUser())) {
				MedicalAdmin createUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getCreateUser());
				bean.setCreateUserName(createUser.getName());
			}
			if(StringUtils.isNotBlank(bean.getHandleUserId())) {
				MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getHandleUserId());
				bean.setHandleUserName(handleUser.getName());
			}
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), bean.getCategory());
			if(null != category){
				bean.setCategoryValue(category.getKeyValue());
			}
			
			// 查询相关订单
			MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer a where a.callId=? ", bean.getId());
			if(null != customer) {
				List<MedicalOrdersStorage> ordersList = this.iBaseDao.getBySql("from MedicalOrdersStorage a where a.customerId=? order by a.createTime desc ", null, customer.getId());
				if(ordersList.size() > 0) {
					bean.setOrdersNo(ordersList.get(0).getSerialNum());
				}
			}
			
			callUntreatedList.add(bean);
		}
		return this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
	}

	@Override
	public Page findCallPendingByPage(MedicalCallPending call,
			MedicalAdmin admin, Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallPending a where 1=1 ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallPending a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? and a.grade=? ");
			countSql.append("and a.hospitalId=? and a.grade=? ");
			objectList.add(admin.getEnterpriseId());
			objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		} else {
			statusSql.append("and a.handleUserId=? ");
			countSql.append("and a.handleUserId=? ");
			objectList.add(admin.getId());
		}
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}	
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallPending> callPendingList = new ArrayList<MedicalCallPending>();
		for(Object obj : page.getResult()) {
			MedicalCallPending bean = (MedicalCallPending) obj;
			if(StringUtils.isNotBlank(bean.getCreateUser())) {
				MedicalAdmin createUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getCreateUser());
				bean.setCreateUserName(createUser.getName());
			}
			MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getHandleUserId());
			bean.setHandleUserName(handleUser.getName());
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), bean.getCategory());
			bean.setCategoryValue(category.getKeyValue());
			
			// 查询相关订单
			MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer a where a.callId=? ", bean.getId());
			if(null != customer) {
				@SuppressWarnings("unchecked")
				List<MedicalOrdersStorage> ordersList = this.iBaseDao.getBySql("from MedicalOrdersStorage a where a.customerId=? order by a.createTime desc ", null, customer.getId());
				if(ordersList.size() > 0) {
					bean.setOrdersNo(ordersList.get(0).getSerialNum());
				}
			}
			callPendingList.add(bean);
		}
		return page;
	}

	@Override
	public Page findCallEndByPage(MedicalCallEnd call, MedicalAdmin admin,
			Map<String, Object> map) {
		
		StringBuilder statusSql = new StringBuilder("from MedicalCallEnd a where 1=1 ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalCallEnd a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? and a.grade=? ");
			countSql.append("and a.hospitalId=? and a.grade=? ");
			objectList.add(admin.getEnterpriseId());
			objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		} else {
			statusSql.append("and a.handleUserId=? ");
			countSql.append("and a.handleUserId=? ");
			objectList.add(admin.getId());
		}
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			countSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			countSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			countSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			countSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			countSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			countSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			countSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			countSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			countSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		// 排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by a." + sortField[0] + " " + sortOrder[0]);
		} else {
			statusSql.append("order by a.createTime desc ");
		}	
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map, "pageIndex", 1), ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalCallEnd> callEndList = new ArrayList<MedicalCallEnd>();
		for(Object obj : page.getResult()) {
			MedicalCallEnd bean = (MedicalCallEnd) obj;
			if(StringUtils.isNotBlank(bean.getCreateUser())) {
				MedicalAdmin createUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getCreateUser());
				bean.setCreateUserName(createUser.getName());
			}
			MedicalAdmin handleUser = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, bean.getHandleUserId());
			bean.setHandleUserName(handleUser.getName());
			MedicalKeyValue category = this.iBaseDao.getValueByKey(admin.getEnterpriseId(), bean.getCategory());
			bean.setCategoryValue(category.getKeyValue());
			
			// 查询相关订单
			MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer a where a.callId=? ", bean.getId());
			if(null != customer) {
				@SuppressWarnings("unchecked")
				List<MedicalOrdersStorage> ordersList = this.iBaseDao.getBySql("from MedicalOrdersStorage a where a.customerId=? order by a.createTime desc ", null, customer.getId());
				if(ordersList.size() > 0) {
					bean.setOrdersNo(ordersList.get(0).getSerialNum());
				}
			}
			
			callEndList.add(bean);
		}
		page.setResult(callEndList);
		
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallUntreatedCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCallUntreated callUntreated = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, cId);
			callUntreated.setCategory(categoryKey);
		}		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallPendingCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCallPending callPending = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, cId);
			callPending.setCategory(categoryKey);
		}		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallEndCategory(String callIds, String categoryKey) {
		String callId[] = callIds.split(",");
		for(String cId : callId){
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, cId);
			callEnd.setCategory(categoryKey);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallUntreatedSta(String callIds, String statusKey, String groupId, String handleUserId) {
		String callId[] = callIds.split(",");
		MedicalCallUntreated callUntreated = null;
		for(String cId : callId) {
			callUntreated = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, cId);
			if(SystemConfig.CALL_TASK_PENDING.equals(statusKey)) {
				MedicalCallPending callPending = new MedicalCallPending();
				BeanUtils.copyProperties(callUntreated, callPending);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callPending);				
			} else if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				// 任务转移，转到另一个人的未处理任务中
				callUntreated.setGroupId(groupId);
				callUntreated.setTransferUser(callUntreated.getHandleUserId());
				callUntreated.setHandleUserId(handleUserId);
				this.iBaseDao.addObject(callUntreated);			
			} else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(callUntreated, callEnd);
				this.iBaseDao.deleteObject(callUntreated);
				this.iBaseDao.addObject(callEnd);
			} else {
				break;
			}
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallPendingSta(String callIds, String statusKey, String groupId, String handleUserId) {
		String callId[] = callIds.split(",");
		MedicalCallPending callPending = null;
		for(String cId : callId) {
			callPending = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, cId);
			if(SystemConfig.CALL_TASK_TRANSFER.equals(statusKey)) {
				// 任务转移，转到另一个人的未处理任务中
				MedicalCallUntreated callUntreated = new MedicalCallUntreated();
				BeanUtils.copyProperties(callPending, callUntreated);
				callUntreated.setGroupId(groupId);
				callUntreated.setTransferUser(callPending.getHandleUserId());
				callUntreated.setHandleUserId(handleUserId);
				this.iBaseDao.addObject(callUntreated);
				this.iBaseDao.deleteObject(callPending);
			}else if(SystemConfig.CALL_TASK_END.equals(statusKey)) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(callPending, callEnd);
				this.iBaseDao.deleteObject(callPending);
				this.iBaseDao.addObject(callEnd);
			}else {
				break;
			}
		}
	}
	
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delUntreatedCall(MedicalCallUntreated callUntreated) {
		if(callUntreated.getId().indexOf(",") != -1){
			String[] callIds = callUntreated.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCallUntreated a where a.id=? ", callId);
				this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callId);
			}
		}else{
			this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callUntreated.getId());
			this.iBaseDao.deleteObject(callUntreated);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delPendingCall(MedicalCallPending callPending) {
		if(callPending.getId().indexOf(",") != -1){
			String[] callIds = callPending.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCallPending a where a.id=? ", callId);
				this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callId);
			}
		}else{
			this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callPending.getId());
			this.iBaseDao.deleteObject(callPending);
		}		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delEndCall(MedicalCallEnd callEnd) {
		if(callEnd.getId().indexOf(",") != -1){
			String[] callIds = callEnd.getId().split(",");
			for(String callId : callIds){
				this.iBaseDao.updateHql("delete from MedicalCallEnd a where a.id=? ", callId);
				this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callId);
			}
		}else{
			this.iBaseDao.updateHql("delete from MedicalCallTelInfo a where a.callId=? ", callEnd.getId());
			this.iBaseDao.deleteObject(callEnd);
		}
	}
	
/*---------------------------------------------打开任务处理--------------------------------------------------*/	

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalCallTelInfo> findTelInfoByCid(String callId) {
		return this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.callId=? ", null, callId);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveTelInfo(MedicalCallTelInfo telInfo) {
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		telInfo.setEid(user.getEnterpriseId());
		this.iBaseDao.saveObject(telInfo);
		
		// 将号码保存到二次进线中间表中
		String handleUserId = "";
		if(SystemConfig.CALL_TASK_UNTREATED.equals(telInfo.getCallStatus()) || SystemConfig.CALL_TASK_TRANSFER.equals(telInfo.getCallStatus())) {
			MedicalCallUntreated callUnt = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, telInfo.getCallId());
			if(StringUtils.isNotBlank(callUnt.getHandleUserId())) {
				handleUserId = callUnt.getHandleUserId();
			}
		} else if(SystemConfig.CALL_TASK_PENDING.equals(telInfo.getCallStatus())) {
			MedicalCallPending callPen = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, telInfo.getCallId());
			if(StringUtils.isNotBlank(callPen.getHandleUserId())) {
				handleUserId = callPen.getHandleUserId();
			}
		} else if(SystemConfig.CALL_TASK_END.equals(telInfo.getCallStatus())) {
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, telInfo.getCallId());
			if(StringUtils.isNotBlank(callEnd.getHandleUserId())) {
				handleUserId = callEnd.getHandleUserId();
			}
		} 		
		if(StringUtils.isNotBlank(handleUserId)) {
			MedicalAdmin admin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, handleUserId);
			List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, admin.getEnterpriseId());
			for(MedicalEnterpriseOutside eOutside : eOutsideList) {
				MedicalCallExtension extension = new MedicalCallExtension();
				extension.setAttendantNo(admin.getUsername());
				extension.setCustomerNum(telInfo.getPhoneNum() + "," + eOutside.getOutSideNum());
				this.iBaseDao.saveObject(extension);
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editSaveTelInfo(MedicalCallTelInfo telInfo) {
		MedicalCallTelInfo bean = (MedicalCallTelInfo) this.iBaseDao.getObject(MedicalCallTelInfo.class, telInfo.getId());
		
		// 更新二次进线中间表
		String handleUserId = "";
		if(SystemConfig.CALL_TASK_UNTREATED.equals(telInfo.getCallStatus()) || SystemConfig.CALL_TASK_TRANSFER.equals(telInfo.getCallStatus())) {
			MedicalCallUntreated callUnt = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, telInfo.getCallId());
			if(StringUtils.isNotBlank(callUnt.getHandleUserId())) {
				handleUserId = callUnt.getHandleUserId();
			}
		} else if(SystemConfig.CALL_TASK_PENDING.equals(telInfo.getCallStatus())) {
			MedicalCallPending callPen = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, telInfo.getCallId());
			if(StringUtils.isNotBlank(callPen.getHandleUserId())) {
				handleUserId = callPen.getHandleUserId();
			}
		} else if(SystemConfig.CALL_TASK_END.equals(telInfo.getCallStatus())) {
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, telInfo.getCallId());
			if(StringUtils.isNotBlank(callEnd.getHandleUserId())) {
				handleUserId = callEnd.getHandleUserId();
			}
		}
		if(StringUtils.isNotBlank(handleUserId)) {
			MedicalAdmin admin = (MedicalAdmin) this.iBaseDao.getObject(MedicalAdmin.class, handleUserId);
			List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, admin.getEnterpriseId());
			for(MedicalEnterpriseOutside eOutside : eOutsideList) {
				MedicalCallExtension extensionBean = (MedicalCallExtension) this.iBaseDao.getObjectByHql("from MedicalCallExtension a where a.customerNum=? ", bean.getPhoneNum() + "," + eOutside.getOutSideNum());
				extensionBean.setCustomerNum(telInfo.getPhoneNum() + "," + eOutside.getOutSideNum());
			}
		}
		
		bean.setPhoneNum(telInfo.getPhoneNum());
		bean.setIsMain(telInfo.getIsMain());
		bean.setType(telInfo.getType());
		bean.setRemarks(telInfo.getRemarks());
		bean.setUpdateTime(new Date());
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delTelInfo(MedicalCallTelInfo telInfo) {
		MedicalCallTelInfo bean = (MedicalCallTelInfo) this.iBaseDao.getObject(MedicalCallTelInfo.class, telInfo.getId());
		List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, bean.getEid());
		for(MedicalEnterpriseOutside eOutside : eOutsideList) {
			this.iBaseDao.updateHql("delete MedicalCallExtension a where a.customerNum=? ", bean.getPhoneNum() + "," + eOutside.getOutSideNum());
		}
		this.iBaseDao.deleteObject(bean);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> findMainNo(String customerId) {
		List<String> contactNoList = new ArrayList<String>();
		MedicalCallTelInfo bean = (MedicalCallTelInfo) this.iBaseDao.getObjectByHql("from MedicalCallTelInfo a where 1=1 and a.customerId=? and a.isMain=? ", customerId, SystemConfig.CUSTOMER_TEL_IS_MAIN);
		if(null != bean) {
			contactNoList.add(bean.getPhoneNum());
		}
		List<MedicalCallTelInfo> list = this.iBaseDao.getBySql("from MedicalCallTelInfo a where 1=1 and a.customerId=? and a.isMain=? ", null, customerId, SystemConfig.CUSTOMER_TEL_NO_MAIN);
		if(list.size() > 0) {
			contactNoList.add(list.get(0).getPhoneNum());
		}
		return contactNoList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalCallTelInfo> findCustomerPhoneNums(String cId) {
		return this.iBaseDao.getBySql("from MedicalCallTelInfo a where 1=1 and a.customerId=? and a.type=? ", null, cId, SystemConfig.CUSTOMER_TEL_PHONE_TYPE);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void recommendTelEdit(MedicalCallTelInfo telInfo) {
		String[] telInfoIds = telInfo.getId().split(",");
		for(String telInfoId : telInfoIds) {
			MedicalCallTelInfo bean = (MedicalCallTelInfo) this.iBaseDao.getObject(MedicalCallTelInfo.class, telInfoId);
			bean.setCallId(telInfo.getCallId());
			bean.setCustomerId(telInfo.getCustomerId());
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String saveRecommendCall(MedicalAdmin admin) {
		MedicalCallUntreated callUntreated = new MedicalCallUntreated();
		String uid = UUID.randomUUID().toString();
		String id = uid.replace("-", "");
		callUntreated.setId(id);
		callUntreated.setSerialNum(SerialNumUtil.code());					// 类别
		callUntreated.setCategory(SystemConfig.CALL_FIRST_TYPE);			// 任务类别：待回访
		callUntreated.setCreateUser(admin.getId());							// 创建者
		callUntreated.setHospitalId(admin.getEnterpriseId()); 				// 医院id
		callUntreated.setAllocationTime(new Date());						// 分配时间
		callUntreated.setIncomingCount(0);									// 进线次数
		if(!SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			callUntreated.setCenterId(admin.getGroup().getPlatform().getId());	// 中心id
			callUntreated.setGroupId(admin.getGroup().getId());					// 组id
		}
		callUntreated.setHandleUserId(admin.getId());						// 处理者id
		callUntreated.setGrade(SystemConfig.CALL_TASK_GRADE_USER);			// 所处等级话务员
		callUntreated.setTransferUser(SystemConfig.CALL_UNT_NO_TRANSFER);	// 未被转移
		this.iBaseDao.addObject(callUntreated);
		return callUntreated.getId();
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateReCallNum(String phoneNum, String callId) {
		this.iBaseDao.updateHql("update MedicalCallUntreated a set a.relatedPhone=? where a.id=? ", phoneNum, callId);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delRecommend(MedicalCallCustomer customer) {
		List<MedicalCallTelInfo> telInfoList = this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.callId=? ", null, customer.getCallId());
		List<MedicalEnterpriseOutside> eOutsideList = this.iBaseDao.getBySql("from MedicalEnterpriseOutside a where a.eid=? ", null, customer.getHospitalId());
		for(MedicalCallTelInfo telInfo : telInfoList) {
			for(MedicalEnterpriseOutside eOutSide : eOutsideList) {
				this.iBaseDao.updateHql("delete from MedicalCallExtension a where a.customerNum=? ", telInfo.getPhoneNum() + "," + eOutSide.getOutSideNum());
			}
			this.iBaseDao.deleteObject(telInfo);
		}
		this.iBaseDao.updateHql("delete from MedicalCallUntreated a where a.id=? ", customer.getCallId());
		this.iBaseDao.deleteObject(customer);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalCallTelInfo> findAllTelInfoByCid(String cId) {
		return this.iBaseDao.getBySql("from MedicalCallTelInfo a where a.customerId=? ", null, cId);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateCustomerBlack(String ids) {
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String[] temp = ids.split("-");
		for (String s : temp) {
			if(StringUtils.isNotBlank(s)){
				MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObject(MedicalCallCustomer.class, s);
				customer.setIsblack("1");
				//将该用户的所有电话加入黑名单
				List<MedicalCallTelInfo> list = findAllTelInfoByCid(s);
				for (MedicalCallTelInfo medicalCallTelInfo : list) {
					MedicalCallBlacklist callBlacklist = new MedicalCallBlacklist();
					callBlacklist.setPhoneNum(medicalCallTelInfo.getPhoneNum());
					callBlacklist.setEid(admin.getEnterpriseId());
					callBlacklist.setCustomer(customer);
					this.iBaseDao.saveObject(callBlacklist);
				}
			}
		}
		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void updateCustomerBlackOut(String ids) {
		String[] temp = ids.split("-");
		for (String s : temp) {
			if(StringUtils.isNotBlank(s)){
				MedicalCallCustomer customer = (MedicalCallCustomer) this.iBaseDao.getObject(MedicalCallCustomer.class, s);
				customer.setIsblack("0");
				this.iBaseDao.updateHql("delete from MedicalCallBlacklist a where a.customer.id=? ", customer.getId());
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalCallStandardArchives> createHealthResult(String result) {
		List<MedicalCallStandardArchives> salist = null;
		if(StringUtils.isNotBlank(result)){
			salist = new LinkedList<MedicalCallStandardArchives>();
			
			String[] temp = result.split("\\|");
			// 判断有没有身高、体重类型的体检、高血压
			Double d1 = null;
			Double d2 = null;
			Double d3 = null;
			Double d4 = null;
			for (String s : temp) {
				String[] temp2 = s.split("-");	//数组下标0为检查类型，1为客户的数据
				if(SystemConfig.HEIGHT.equals(temp2[0])){
					d1 = Double.valueOf(temp2[1]);
				}
				if(SystemConfig.WEIGHT.equals(temp2[0])){
					d2 = Double.valueOf(temp2[1]);
				}
				if(SystemConfig.DBP.equals(temp2[0])){
					d3 = Double.valueOf(temp2[1]);
				}
				if(SystemConfig.SBP.equals(temp2[0])){
					d4 = Double.valueOf(temp2[1]);
				}
			}
			
			if(d1 != null && d2 != null){
				double bodyIndex = d2/(d1*d1);
				List<MedicalCallStandardArchives> list = this.iBaseDao.getBySql("from MedicalCallStandardArchives a where a.typeKey=? ", null, SystemConfig.HEIGHT_WEIGHT);
				for (MedicalCallStandardArchives standardArchives : list) {
					// 判断数值范围
					if(bodyIndex >= standardArchives.getScope1() && bodyIndex < standardArchives.getScope2()){
						salist.add(standardArchives);
					}
				}
			}
			
			if(d3 != null && d4 != null){
				MedicalCallStandardArchives archives1 = (MedicalCallStandardArchives) this.iBaseDao.getObjectByHql("from MedicalCallStandardArchives a where a.typeKey=? ", SystemConfig.DBP);
				MedicalCallStandardArchives archives2 = (MedicalCallStandardArchives) this.iBaseDao.getObjectByHql("from MedicalCallStandardArchives a where a.typeKey=? ", SystemConfig.SBP);
				if(d3 >= archives1.getScope2() && d4 >= archives2.getScope2() ){
					MedicalCallStandardArchives archives = new MedicalCallStandardArchives();
					archives.setTypeValue("血压");
					archives.setExplain("高血压");
					salist.add(archives);
				}
				if((d3 >= archives1.getScope1() && d3 < archives1.getScope2()) && (d4 >= archives2.getScope1() && d4 < archives2.getScope2())){
					MedicalCallStandardArchives archives = new MedicalCallStandardArchives();
					archives.setTypeValue("血压");
					archives.setExplain("正常水平");
					salist.add(archives);
				}
			}
			
			for (String s : temp) {
				String[] temp2 = s.split("-");	//数组下标0为检查类型，1为客户的数据
				List<MedicalCallStandardArchives> list = this.iBaseDao.getBySql("from MedicalCallStandardArchives a where a.typeKey=? ", null, temp2[0]);
				for (MedicalCallStandardArchives standardArchives : list) {
					double data = Double.valueOf(temp2[1]);
					if(standardArchives.getTypeKey().equals(SystemConfig.BIL) || standardArchives.getTypeKey().equals(SystemConfig.BLD)
							|| standardArchives.getTypeKey().equals(SystemConfig.GLU)
							|| standardArchives.getTypeKey().equals(SystemConfig.KET)
							|| standardArchives.getTypeKey().equals(SystemConfig.LEU)
							|| standardArchives.getTypeKey().equals(SystemConfig.PRO)){
						// 判断阴性、阳性
						if(data == standardArchives.getScope1()){
							salist.add(standardArchives);
						}
					}else if(!standardArchives.getTypeKey().equals(SystemConfig.HEIGHT_WEIGHT) 
							&& !standardArchives.getTypeKey().equals(SystemConfig.DBP)
							&& !standardArchives.getTypeKey().equals(SystemConfig.SBP)){
						// 判断数值范围
						if(data >= standardArchives.getScope1() && data < standardArchives.getScope2()){
							salist.add(standardArchives);
						}
					}
				}
			}
		}
		return salist;
	}

	@Override
	public Workbook untExportAll(MedicalAdmin admin, MedicalCallUntreated call)
			throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallUntreated a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? and a.grade=? ");
			objectList.add(admin.getEnterpriseId());
			objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		} else {
			statusSql.append("and a.handleUserId=? ");
			objectList.add(admin.getId());
		}
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		@SuppressWarnings("unchecked")
		List<MedicalCallUntreated> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallUntreated medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@Override
	public Workbook untExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallUntreated medicalCall = (MedicalCallUntreated) this.iBaseDao
						.getObject(MedicalCallUntreated.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@Override
	public Workbook penExportAll(MedicalAdmin admin, MedicalCallPending call)
			throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallPending a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? and a.grade=? ");
			objectList.add(admin.getEnterpriseId());
			objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		} else {
			statusSql.append("and a.handleUserId=? ");
			objectList.add(admin.getId());
		}
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		@SuppressWarnings("unchecked")
		List<MedicalCallPending> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallPending medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@Override
	public Workbook penExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallPending medicalCall = (MedicalCallPending) this.iBaseDao
						.getObject(MedicalCallPending.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@Override
	public Workbook endExportAll(MedicalAdmin admin, MedicalCallEnd call)
			throws Exception {
		StringBuilder statusSql = new StringBuilder("from MedicalCallEnd a where 1=1 ");
		
		List<Object> objectList = new ArrayList<Object>();
		if(SystemConfig.SUPER_ADMIN.equals(admin.getIsSuper())) {
			statusSql.append("and a.hospitalId=? and a.grade=? ");
			objectList.add(admin.getEnterpriseId());
			objectList.add(SystemConfig.CALL_TASK_GRADE_USER);
		} else {
			statusSql.append("and a.handleUserId=? ");
			objectList.add(admin.getId());
		}
		
		if(null != call.getStime()){
			statusSql.append("and a.createTime >= ? ");
			objectList.add(call.getStime());
		}
		
		if(null != call.getEtime()){
			statusSql.append("and a.createTime <= ? ");
			objectList.add(call.getEtime());
		}
		
		if(StringUtils.isNotBlank(call.getSerialNum())){
			statusSql.append("and a.serialNum = ? ");
			objectList.add(call.getSerialNum());
		}
		
		if(StringUtils.isNotBlank(call.getCategory())){
			statusSql.append("and a.category = ? ");
			objectList.add(call.getCategory());
		}
		
		if(StringUtils.isNotBlank(call.getRelatedPhone())){
			statusSql.append("and a.relatedPhone = ? ");
			objectList.add(call.getRelatedPhone());
		}
		
		if(StringUtils.isNotBlank(call.getOutsideNum())){
			statusSql.append("and a.outsideNum = ? ");
			objectList.add(call.getOutsideNum());
		}
		
		if(StringUtils.isNotBlank(call.getCreateUser())){
			statusSql.append("and a.createUser = ? ");
			objectList.add(call.getCreateUser());
		}
		
		if(null != call.getUstime()){
			statusSql.append("and a.updateTime >= ? ");
			objectList.add(call.getUstime());
		}
		
		if(null != call.getUetime()){
			statusSql.append("and a.updateTime <= ? ");
			objectList.add(call.getUetime());
		}
		
		statusSql.append("order by a.createTime desc ");
		
		int i = 0;
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext().getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}else{}
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		@SuppressWarnings("unchecked")
		List<MedicalCallEnd> medicalCallList = this.iBaseDao.getBySql(statusSql.toString(), null,objectList.toArray());
		//遍历所有未分配任务
		for (MedicalCallEnd medicalCall : medicalCallList) {
			i++;
			Row currentRow = sheet.createRow(i);
			//编号
			if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
				currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
			}else{}
			//话务类型
			if(StringUtils.isNotBlank(medicalCall.getCategory())){
				MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(admin.getEnterpriseId(), medicalCall.getCategory());
				currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
			}else{}
			//创建时间
			if(null != medicalCall.getCreateTime()){
				currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//更新时间
			if(null != medicalCall.getUpdateTime()){
				currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
			}else{}
			//呼入次数
			if(null != medicalCall.getIncomingCount()) {
				currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
			}
			//创建者
			if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
				MedicalAdmin user = (MedicalAdmin)this.iBaseDao
						.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
				currentRow.createCell(5).setCellValue(user.getName());
			}else{}
			//备注
			if(StringUtils.isNotBlank(medicalCall.getRemark())){
				currentRow.createCell(6).setCellValue(medicalCall.getRemark());
			}else{}
			//外线号码
			if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
				currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
			}else{}
			//相关手机号
			if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
				currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
			}else{}
		}
		return workbook;
	}

	@Override
	public Workbook endExportCurrent(String ids) throws Exception {
		//获取模板
		String AbsolutelyPath = ServletActionContext.getServletContext()
				.getRealPath(SystemConfig.EXPORT_CALL_UNALLOCATED_TASK);
		File file = new File(AbsolutelyPath);
		if(!file.exists()){
			throw new FileNotFoundException("没有找到excel文件");
		}	
		InputStream is = new FileInputStream(file);
		Workbook workbook = WorkbookFactory.create(is);
		is.close();
		Sheet sheet = workbook.getSheetAt(0);
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获得本页的未分配任务
		if(StringUtils.isNotBlank(ids)){
			String [] taskIds =  ids.split(",");
			for(int j=0;j<taskIds.length;j++){
				// 获得记录
				MedicalCallEnd medicalCall = (MedicalCallEnd) this.iBaseDao
						.getObject(MedicalCallEnd.class,taskIds[j]);
				Row currentRow = sheet.createRow(j+1);
				//编号
				if(StringUtils.isNotBlank(medicalCall.getSerialNum())){
					currentRow.createCell(0).setCellValue(medicalCall.getSerialNum());
				}else{}
				//话务类型
				if(StringUtils.isNotBlank(medicalCall.getCategory())){
					MedicalKeyValue keyVal = (MedicalKeyValue)this.iBaseDao.getValueByKey(user.getEnterpriseId(), medicalCall.getCategory());
					currentRow.createCell(1).setCellValue(keyVal.getKeyValue());
				}else{}
				//创建时间
				if(null != medicalCall.getCreateTime()){
					currentRow.createCell(2).setCellValue(DateFormatUtils.format(medicalCall.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//更新时间
				if(null != medicalCall.getUpdateTime()){
					currentRow.createCell(3).setCellValue(DateFormatUtils.format(medicalCall.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
				}else{}
				//呼入次数
				if(null != medicalCall.getIncomingCount()) {
					currentRow.createCell(4).setCellValue(medicalCall.getIncomingCount());
				}
				//创建者
				if(StringUtils.isNotBlank(medicalCall.getCreateUser())){
					MedicalAdmin admin = (MedicalAdmin)this.iBaseDao
							.getObject(MedicalAdmin.class, medicalCall.getCreateUser());
					currentRow.createCell(5).setCellValue(admin.getName());
				}else{}
				//备注
				if(StringUtils.isNotBlank(medicalCall.getRemark())){
					currentRow.createCell(6).setCellValue(medicalCall.getRemark());
				}else{}
				//外线号码
				if(StringUtils.isNotBlank(medicalCall.getOutsideNum())){
					currentRow.createCell(7).setCellValue(medicalCall.getOutsideNum());
				}
				//相关手机号
				if(StringUtils.isNotBlank(medicalCall.getRelatedPhone())){
					currentRow.createCell(8).setCellValue(medicalCall.getRelatedPhone());
				}else{}
			}
		}else{}
		return workbook;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MedicalKeyValue> findCallStatusKey(MedicalAdmin admin, String typeName) {
		List<MedicalKeyValue> keyValList = this.iBaseDao.getBySql("from MedicalKeyValue a where a.hospitalId=? and a.systemType=? and a.typeName=? ", null, admin.getEnterpriseId(), SystemConfig.CM_SYSTEM_TYPE, typeName);
		Iterator<MedicalKeyValue> keyValIter = keyValList.iterator();
		while(keyValIter.hasNext()) {
			MedicalKeyValue keyVal = keyValIter.next();
			if(SystemConfig.CALL_TASK_UNALLOCATED.equals(keyVal.getKeyName()) || 
					SystemConfig.CALL_TASK_UNTREATED.equals(keyVal.getKeyName()) || 
					SystemConfig.CALL_TASK_TERMINATE.equals(keyVal.getKeyName())) {
				keyValIter.remove();
			}
		}
		return keyValList;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editCallStatus(MedicalAdmin admin, MedicalCallUntreated callUnt) {
		
		if(SystemConfig.CALL_TASK_UNALLOCATED.equals(callUnt.getCallStatus())) {		// 未分配
			MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, callUnt.getId());
			if(SystemConfig.CALL_TASK_PENDING.equals(callUnt.getStatusKey())) {
				MedicalCallPending callPen = new MedicalCallPending();
				BeanUtils.copyProperties(call, callPen);
				this.iBaseDao.addObject(callPen);
				this.iBaseDao.deleteObject(call);
			} else if(SystemConfig.CALL_TASK_END.equals(callUnt.getStatusKey())) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(call, callEnd);
				this.iBaseDao.addObject(callEnd);
				this.iBaseDao.deleteObject(call);
			} else {
				MedicalCallUntreated beanUnt = new MedicalCallUntreated();
				BeanUtils.copyProperties(call, beanUnt);
				beanUnt.setGroupId(callUnt.getGroupId());
				beanUnt.setHandleUserId(callUnt.getHandleUserId());
				beanUnt.setGrade(SystemConfig.CALL_TASK_GRADE_USER);
				beanUnt.setTransferUser(admin.getId());
				this.iBaseDao.addObject(beanUnt);
				this.iBaseDao.deleteObject(call);
			}
			
		} else if(SystemConfig.CALL_TASK_UNTREATED.equals(callUnt.getCallStatus())) {		// 未处理
			MedicalCallUntreated beanUnt = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, callUnt.getId());
			
			if(SystemConfig.CALL_TASK_PENDING.equals(callUnt.getStatusKey())) {
				MedicalCallPending callPen = new MedicalCallPending();
				BeanUtils.copyProperties(beanUnt, callPen);
				this.iBaseDao.addObject(callPen);
				this.iBaseDao.deleteObject(beanUnt);
			} else if(SystemConfig.CALL_TASK_END.equals(callUnt.getStatusKey())) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(beanUnt, callEnd);
				this.iBaseDao.addObject(callEnd);
				this.iBaseDao.deleteObject(beanUnt);
			} else {
				beanUnt.setGroupId(callUnt.getGroupId());
				beanUnt.setHandleUserId(callUnt.getHandleUserId());
				beanUnt.setTransferUser(admin.getId());
			}
			
		} else if(SystemConfig.CALL_TASK_PENDING.equals(callUnt.getCallStatus())) {			// 待处理
			MedicalCallPending callPen = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, callUnt.getId());
			
			if(SystemConfig.CALL_TASK_END.equals(callUnt.getStatusKey())) {
				MedicalCallEnd callEnd = new MedicalCallEnd();
				BeanUtils.copyProperties(callPen, callEnd);
				this.iBaseDao.addObject(callEnd);
				this.iBaseDao.deleteObject(callPen);
				
			} else if(SystemConfig.CALL_TASK_TRANSFER.equals(callUnt.getStatusKey())) {
				MedicalCallUntreated beanUnt = new MedicalCallUntreated();
				BeanUtils.copyProperties(callPen, beanUnt);
				beanUnt.setGroupId(callUnt.getGroupId());
				beanUnt.setHandleUserId(callUnt.getHandleUserId());
				beanUnt.setTransferUser(admin.getId());
				beanUnt.setGrade(SystemConfig.CALL_TASK_GRADE_USER);
				this.iBaseDao.addObject(beanUnt);
				this.iBaseDao.deleteObject(callPen);
			} else {
			}
			
		} else {}
	}

	@Override
	public String validNumUnique(MedicalCallTelInfo telInfo) {
		MedicalCallTelInfo bean = (MedicalCallTelInfo) this.iBaseDao.getObjectByHql("from MedicalCallTelInfo a where a.eid=? and a.phoneNum=? ", telInfo.getEid(), telInfo.getPhoneNum());
		StringBuilder callId = new StringBuilder("");
		if(null != bean) {
			callId.append(bean.getCallId());
		}
		return callId.toString();
	}

	@Override
	public String findCallStatus(String callId) {
		StringBuilder callStatus = new StringBuilder("");
		MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, callId);
		if(null != call) {
			callStatus.append(SystemConfig.CALL_TASK_UNALLOCATED);
		} else {
			MedicalCallUntreated callUnt = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, callId);
			if(null != callUnt && SystemConfig.CALL_UNT_NO_TRANSFER.equals(callUnt.getTransferUser())) {
				callStatus.append(SystemConfig.CALL_TASK_UNTREATED);
			} else if(null != callUnt) {
				callStatus.append(SystemConfig.CALL_TASK_TRANSFER);
			} else {
				MedicalCallPending callPen = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, callId);
				if(null != callPen) {
					callStatus.append(SystemConfig.CALL_TASK_PENDING);
				} else {
					MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, callId);
					if(null != callEnd) {
						callStatus.append(SystemConfig.CALL_TASK_END);
					} else {
						MedicalCallTerminate callTer = (MedicalCallTerminate) this.iBaseDao.getObject(MedicalCallTerminate.class, callId);
						if(null != callTer) {
							callStatus.append(SystemConfig.CALL_TASK_TERMINATE);
						}
					}
				}
			}
		}
		return callStatus.toString();
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void mergePhone(String callStatus, String callId, String openCallId) {
		
		// 删除当前任务，将该任务的号码合并到要打开的任务中。
		if(SystemConfig.CALL_TASK_UNALLOCATED.equals(callStatus) || SystemConfig.CALL_TASK_LIST_UNALLOCATE.equals(callStatus)) {
			MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, callId);
			this.iBaseDao.deleteObject(call);
			
		} else if(SystemConfig.CALL_TASK_UNTREATED.equals(callStatus)) {
			MedicalCallUntreated callUnt = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, callId);
			this.iBaseDao.deleteObject(callUnt);
			
		} else if(SystemConfig.CALL_TASK_PENDING.equals(callStatus)) {
			MedicalCallPending callPen = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, callId);
			this.iBaseDao.deleteObject(callPen);
			
		} else if(SystemConfig.CALL_TASK_TRANSFER.equals(callStatus)) {
			MedicalCallUntreated callUnt = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, callId);
			this.iBaseDao.deleteObject(callUnt);
			
		} else if(SystemConfig.CALL_TASK_END.equals(callStatus)) {
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, callId);
			this.iBaseDao.deleteObject(callEnd);
			
		} else if(SystemConfig.CALL_TASK_TERMINATE.equals(callStatus)) {
			MedicalCallTerminate callTer = (MedicalCallTerminate) this.iBaseDao.getObject(MedicalCallTerminate.class, callId);
			this.iBaseDao.deleteObject(callTer);
			
		}
		this.iBaseDao.updateHql("update MedicalCallTelInfo a set a.callId=? where a.callId=? ", openCallId, callId);		// 更新当前任务的号码的任务id为，要打开的任务的id
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void mergeCall(String callStatus, String callId, String customerId, String openCallId) {

		// 删除当前任务，将该任务的号码合并到要打开的任务中。
		if(SystemConfig.CALL_TASK_UNALLOCATED.equals(callStatus) || SystemConfig.CALL_TASK_LIST_UNALLOCATE.equals(callStatus)) {
			MedicalCall call = (MedicalCall) this.iBaseDao.getObject(MedicalCall.class, callId);
			this.iBaseDao.deleteObject(call);
			
		} else if(SystemConfig.CALL_TASK_UNTREATED.equals(callStatus)) {
			MedicalCallUntreated callUnt = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, callId);
			this.iBaseDao.deleteObject(callUnt);
			
		} else if(SystemConfig.CALL_TASK_PENDING.equals(callStatus)) {
			MedicalCallPending callPen = (MedicalCallPending) this.iBaseDao.getObject(MedicalCallPending.class, callId);
			this.iBaseDao.deleteObject(callPen);
			
		} else if(SystemConfig.CALL_TASK_TRANSFER.equals(callStatus)) {
			MedicalCallUntreated callUnt = (MedicalCallUntreated) this.iBaseDao.getObject(MedicalCallUntreated.class, callId);
			this.iBaseDao.deleteObject(callUnt);
			
		} else if(SystemConfig.CALL_TASK_END.equals(callStatus)) {
			MedicalCallEnd callEnd = (MedicalCallEnd) this.iBaseDao.getObject(MedicalCallEnd.class, callId);
			this.iBaseDao.deleteObject(callEnd);
			
		} else if(SystemConfig.CALL_TASK_TERMINATE.equals(callStatus)) {
			MedicalCallTerminate callTer = (MedicalCallTerminate) this.iBaseDao.getObject(MedicalCallTerminate.class, callId);
			this.iBaseDao.deleteObject(callTer);
			
		}
		MedicalCallCustomer openCustomer = (MedicalCallCustomer) this.iBaseDao.getObjectByHql("from MedicalCallCustomer a where a.callId=? ", openCallId);
		
		// 合并订单
		this.iBaseDao.updateHql("update MedicalOrdersStorage a set a.customerId=? where a.customerId=? ", openCustomer.getId(), customerId);
		
		// 联系记录
		this.iBaseDao.updateHql("update MedicalCallContactRecord a set a.customerId=? where a.customerId=? ", openCustomer.getId(), customerId);
		
		// 客户提醒
		this.iBaseDao.updateHql("update MedicalCallCustomerReminder a set a.customerId=? where a.customerId=? ", openCustomer.getId(), customerId);
		
		// 短信
		this.iBaseDao.updateHql("update MedicalCallSms a set a.customerId=? where a.customerId=? ", openCustomer.getId(), customerId);
		
		// 健康档案
		this.iBaseDao.updateHql("update MedicalCallCustomerHealth a set a.customerId=? where a.customerId=? ", openCustomer.getId(), customerId);
		
		// 推荐人
		this.iBaseDao.updateHql("update MedicalCallCustomer a set a.recommendId=? where a.recommendId=? ", openCustomer.getId(), customerId);
		
		// 删除当前客户资料
		this.iBaseDao.updateHql("delete MedicalCallCustomer a where a.id=? ", customerId);
		
		// 更新客户号码客户id
		this.iBaseDao.updateHql("update MedicalCallTelInfo a set a.customerId=? where a.customerId=? ", openCustomer.getId(), customerId);
		
		// 更新客户号码任务id
		this.iBaseDao.updateHql("update MedicalCallTelInfo a set a.callId=? where a.callId=? ", openCallId, callId);		// 更新当前任务的号码的任务id为，要打开的任务的id
		
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void saveNewTelInfo(MedicalCallTelInfo telInfo) {
		this.iBaseDao.saveObject(telInfo);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void editNewTelInfo(MedicalCallTelInfo telInfo) {
		MedicalCallTelInfo bean = (MedicalCallTelInfo) this.iBaseDao.getObject(MedicalCallTelInfo.class, telInfo.getId());
		bean.setPhoneNum(telInfo.getPhoneNum());
		bean.setType(telInfo.getType());
		bean.setIsMain(telInfo.getIsMain());
		bean.setRemarks(telInfo.getRemarks());
		bean.setUpdateTime(new Date());
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void delNewTelInfo(MedicalCallTelInfo telInfo) {
		this.iBaseDao.deleteObject(telInfo);
	}

}
